<template>
  <div class="bubble-row" :class="role">
    <img
      v-if="role === 'ai'"
      class="avatar left"
      src="https://cdn.jsdelivr.net/gh/Chanzhaoyu/chatgpt-web@main/public/logo.png"
      alt="AI"
    />
    <div :class="['bubble', role, { error }]">
      <template v-if="!error">
        <VueMarkdown :markdown="content" :remark-plugins="[remarkGfm]" />
        <span v-if="loading" class="loading-bar"> <span class="dot">.</span><span class="dot">.</span><span class="dot">.</span> </span>
      </template>
      <template v-else>
        <span class="error-text">{{ content }}</span>
      </template>
    </div>
    <img
      v-if="role === 'user'"
      class="avatar right"
      src="https://randomuser.me/api/portraits/men/32.jpg"
      alt="User"
    />
  </div>
</template>

<script setup lang="ts">
import { VueMarkdown } from '@crazydos/vue-markdown'
import remarkGfm from 'remark-gfm'
defineProps<{
  content: string
  role: 'user' | 'ai'
  error?: boolean
  loading?: boolean
}>()
</script>

<style scoped>
.bubble-row {
  display: flex;
  align-items: flex-start;
  margin-bottom: 18px;
}
.bubble-row.ai {
  flex-direction: row;
  justify-content: flex-start;
}
.bubble-row.user {
  flex-direction: row-reverse;
  justify-content: flex-end;
}
.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #f0f0f0;
  object-fit: cover;
  border: 1.5px solid #e5e7eb;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex-shrink: 0; /* 防止头像被压缩 */
}
.avatar.left {
  margin-right: 12px;
  margin-left: 0;
}
.avatar.right {
  margin-left: 12px;
  margin-right: 0;
}
.bubble {
  max-width: 70%;
  padding: 14px 18px;
  border-radius: 18px;
  word-break: break-word;
  font-size: 15px;
  line-height: 1.7;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  display: inline-block;
  min-width: 60px;
  position: relative;
}
.user {
  background: #1677ff;
  color: #fff;
  border-bottom-right-radius: 4px;
}
.user :deep(*) {
  color: #fff !important;
}
.ai {
  background: #fff;
  color: #222;
  border-bottom-left-radius: 4px;
  border: 1px solid #e5e7eb;
}
.bubble.error {
  border: 1.5px solid #ff4d4f;
  background: #fff1f0;
}
.error-text {
  color: #ff4d4f;
  font-weight: bold;
}
.loading-bar {
  display: inline-block;
  margin-left: 6px;
  font-size: 18px;
  color: #bbb;
  letter-spacing: 1px;
  vertical-align: middle;
  animation: blink 1.2s infinite steps(1, end);
}
.dot {
  animation: blink 1.2s infinite alternate;
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* Markdown内容美化 */
.bubble :deep(h1),
.bubble :deep(h2),
.bubble :deep(h3),
.bubble :deep(h4),
.bubble :deep(h5),
.bubble :deep(h6) {
  margin: 8px 0 6px 0;
  font-weight: bold;
  line-height: 1.4;
}
.bubble :deep(p) {
  margin: 6px 0;
}
.bubble :deep(ul),
.bubble :deep(ol) {
  margin: 6px 0 6px 24px; /* 增加列表缩进 */
  padding: 0;
}
.bubble :deep(li) {
  margin: 4px 0;
  line-height: 1.6;
}
.bubble :deep(code) {
  background: rgba(235, 237, 240, 0.6);
  color: #d6336c;
  padding: 3px 7px;
  border-radius: 4px;
  font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
  font-size: 14px;
}
.bubble :deep(pre) {
  background: rgba(235, 237, 240, 0.6);
  color: #222;
  border-radius: 6px;
  padding: 14px;
  margin: 10px 0;
  font-size: 14px;
  overflow-x: auto;
  line-height: 1.6;
}
.bubble :deep(table) {
  border-collapse: collapse;
  margin: 10px 0;
  width: 100%;
  font-size: 14px;
}
.bubble :deep(th),
.bubble :deep(td) {
  border: 1px solid #ddd;
  padding: 8px 12px;
  text-align: left;
}
.bubble :deep(th) {
  background: #f2f2f2;
  font-weight: bold;
}
.bubble :deep(a) {
  color: #1677ff;
  text-decoration: underline;
}

.bubble :deep(blockquote) {
  border-left: 4px solid #cfcfcf;
  padding-left: 12px;
  margin: 10px 0;
  color: #666;
  font-style: italic;
}

</style> 